/**
 * https://leetcode.cn/problems/leaf-similar-trees/
 */
function leafSimilar(root1: TreeNode | null, root2: TreeNode | null): boolean {
  const root1Values: number[] = [];
  const root2Values: number[] = [];

  const isLeave = (node: TreeNode) => {
    return node.left === null && node.right === null;
  };

  const prevOrder = (node: TreeNode | null, values: number[]) => {
    if (node === null) return;
    if (isLeave(node)) {
      values.push(node.val);
    }
    prevOrder(node.left, values);
    prevOrder(node.right, values);
  };

  const isSame = (values1: number[], values2: number[]) => {
    const v1Length = values1.length;
    const v2Length = values2.length;

    if (v1Length !== v2Length) return false;

    for (let i = 0; i < v1Length; i++) {
      if (values1[i] !== values2[i]) return false;
    }

    return true;
  };

  prevOrder(root1, root1Values);
  prevOrder(root2, root2Values);

  return isSame(root1Values, root2Values);
}
